/**
 * Created by SJay on 2017/9/28.
 */
import axios from "axios"
import API from "../../api/cjy-edit-api"
import creatUuid from "../UUID"

// var  sessionId  =  '36a0e35cdc7144d4a4336c3b9e876daa'
var ttId = ''

function getCookie(c_name) {
  var c_start
  var c_end
  if (document.cookie.length > 0) {
    c_start = document.cookie.indexOf(c_name + "=")
    if (c_start != -1) {
      c_start = c_start + c_name.length + 1
      c_end = document.cookie.indexOf(";", c_start)
      if (c_end == -1) c_end = document.cookie.length
      return unescape(document.cookie.substring(c_start, c_end))
    }
  }
  return ""
}

const getInforFun = (url, data, method = "get", header = false) => {
  var api = API()
  var url = api[url]
  var config = {
    method: method,
  }
  if (method == "get" || method == 'delete') {
    config.url = url
    config.params = data
  }
  else if (method == "post") {
    config.url = url
    config.data = data
  }
  else {
    config.url = url
    config.data = data
  }

  if (header && header != 'undefined') {
    config.headers = {sessionId: header}
  }
  return axios(config);
}

export const queryCommendList = async (data) => { //获取商品分类列表的请求
  var api = API()
  var storeCode = api.shopCode
  var enterpriseId = api.enterpriseId
  var sessionId = ttId || getCookie('sessionId')
  console.log('bbbbbbbbb', sessionId)
  data.token = new Date().getTime()
  data.storeCode = storeCode
  return await getInforFun('commend', data, 'get', sessionId)
}
export const addCommendList = async (data) => { //新增商品分类列表的请求
  var api = API()
  var enterpriseId = api.enterpriseId
  var sessionId = ttId || getCookie('sessionId')

  return await getInforFun('commend', data, 'post', sessionId)
}
export const updataCommendList = async (data) => { //修改商品分类列表的请求
  var api = API()
  var enterpriseId = api.enterpriseId
  var sessionId = ttId || getCookie('sessionId')
  return await getInforFun('commend', data, 'put',sessionId)
}
export const deleteCommendList = async (data) => { //删除商品分类列表的请求
  var api = API()
  var storeCode = api.shopCode
  var enterpriseId = api.enterpriseId
  var sessionId = ttId || getCookie('sessionId')
  console.log('bbbbbbbbb', sessionId)
  data.token = new Date().getTime()
  data.storeCode = storeCode
  return await getInforFun('commend', data, 'delete', sessionId)
}
